
/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2019原作者。
 *
 *根据Apache许可证2.0版（“许可证”）获得许可；
 *除非符合许可证，否则不得使用此文件。
 *您可以在以下地址获取许可证副本：
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证分发的内容是按“原样”分发的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限和
 *许可证下的限制。
 */

package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;

/**
 * Factory hook that allows for custom modification of new bean instances &mdash;
 * for example, checking for marker interfaces or wrapping beans with proxies.
 *
 * <p>Typically, post-processors that populate beans via marker interfaces
 * or the like will implement {@link #postProcessBeforeInitialization},
 * while post-processors that wrap beans with proxies will normally
 * implement {@link #postProcessAfterInitialization}.
 *
 * <h3>Registration</h3>
 * <p>An {@code ApplicationContext} can autodetect {@code BeanPostProcessor} beans
 * in its bean definitions and apply those post-processors to any beans subsequently
 * created. A plain {@code BeanFactory} allows for programmatic registration of
 * post-processors, applying them to all beans created through the bean factory.
 *
 * <h3>Ordering</h3>
 * <p>{@code BeanPostProcessor} beans that are autodetected in an
 * {@code ApplicationContext} will be ordered according to
 * {@link org.springframework.core.PriorityOrdered} and
 * {@link org.springframework.core.Ordered} semantics. In contrast,
 * {@code BeanPostProcessor} beans that are registered programmatically with a
 * {@code BeanFactory} will be applied in the order of registration; any ordering
 * semantics expressed through implementing the
 * {@code PriorityOrdered} or {@code Ordered} interface will be ignored for
 * programmatically registered post-processors. Furthermore, the
 * {@link org.springframework.core.annotation.Order @Order} annotation is not
 * taken into account for {@code BeanPostProcessor} beans.
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 10.10.2003
 * @see InstantiationAwareBeanPostProcessor
 * @see DestructionAwareBeanPostProcessor
 * @see ConfigurableBeanFactory#addBeanPostProcessor
 * @see BeanFactoryPostProcessor
 */
/**
 *允许自定义修改新bean实例的Factory钩子&mdash；
 *例如，检查标记接口或用代理包装bean。
 *
 *＜p＞通常，通过标记接口填充bean的后处理器
 *等等将实现{@link#postProcessBeforeInitialization}，
 *而用代理包装bean的后处理器通常
 *实现｛@link#postProcessAfterInitialization｝。
 *
 *<h3>注册</h3>
 *＜p＞｛@code ApplicationContext｝可以自动检测｛@codeBeanPostProcessor｝bean
 *并随后将这些后处理器应用于任何bean
 *创建。一个简单的｛@code BeanFactory｝允许编程注册
 *后处理器，将它们应用于通过bean工厂创建的所有bean。
 *
 *<h3>订购</h3>
 *＜p＞｛@code BeanPostProcessor｝在
 *｛@code ApplicationContext｝将根据
 *｛@link org.springframework.core.PriorityOrdered｝和
 *{@link.org.springframework.core.Ordered}语义。相反
 *｛@code BeanPostProcessor｝以编程方式注册到
 *｛@code BeanFactory｝将按注册顺序应用；任何订货
 *通过实现
 *｛@code PriorityOrdered｝或｛@ccode Ordered｝｝接口将被忽略
 *以编程方式注册的后处理器。此外
 *｛@link org.springframework.core.annotation.Order@Order｝注释不是
 *考虑到｛@code BeanPostProcessor｝bean。
 *
 *@作者Juergen Hoeller
 *@作者Sam Brannen
 *@自2003年10月10日
 *@参见InstantiationAwareBeanPostProcessor
 *@参见DestructionAwareBeanPostProcessor
 *@参见可配置的BeanFactory#addBeanPostProcessor
 *@参见BeanFactoryPostProcessor
 */
public interface BeanPostProcessor {

	/**
	 * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 */
	/**
	 *在＜/i＞任何bean之前，将这个｛@code BeanPostProcessor｝应用于给定的新bean实例＜i＞
	 *初始化回调（如InitializingBean的｛@code afterPropertiesSet｝
	 *或自定义init方法）。bean将已经用属性值填充。
	 *返回的bean实例可能是原始bean的包装器。
	 *＜p＞默认实现按原样返回给定的｛@code bean｝。
	 *@param bean新bean实例
	 *@param beanName bean的名称
	 *@返回要使用的bean实例，无论是原始实例还是包装的实例；
	 *如果｛@code null｝，则不会调用后续的BeanPostProcessors
	 *出现错误时@throws org.springframework.beans.BeansException
	 *@see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
	 * instance and the objects created by the FactoryBean (as of Spring 2.0). The
	 * post-processor can decide whether to apply to either the FactoryBean or created
	 * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
	 * <p>This callback will also be invoked after a short-circuiting triggered by a
	 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
	 * in contrast to all other {@code BeanPostProcessor} callbacks.
	 * <p>The default implementation returns the given {@code bean} as-is.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one;
	 * if {@code null}, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 * @see org.springframework.beans.factory.FactoryBean
	 */
	/**
	 *将此｛@code BeanPostProcessor｝应用于任何bean之后的给定新bean实例＜i＞
	 *初始化回调（如InitializingBean的｛@code afterPropertiesSet｝
	 *或自定义init方法）。bean将已经用属性值填充。
	 *返回的bean实例可能是原始bean的包装器。
	 *＜p＞如果是FactoryBean，将为FactoryBean调用此回调
	 *实例和FactoryBean创建的对象（从Spring 2.0开始）
	 *后处理器可以决定是应用于FactoryBean还是创建
	 *对象或通过相应的｛@codebean instanceofFactoryBean｝检查两者。
	 *＜p＞在由
	 *｛@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantation｝方法，
	 *与所有其他｛@code BeanPostProcessor｝回调不同。
	 *＜p＞默认实现按原样返回给定的｛@code bean｝。
	 *@param bean新bean实例
	 *@param beanName bean的名称
	 *@返回要使用的bean实例，无论是原始实例还是包装的实例；
	 *如果｛@code null｝，则不会调用后续的BeanPostProcessors
	 *出现错误时@throws org.springframework.beans.BeansException
	 *@see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 *@参见org.springframework.beans.factory.FactoryBean
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}
